package de.koller.million.grid;

import de.koller.million.card.frame.util.NewsModel;
import de.koller.million.grid.engine.EngineRoot;
import de.koller.million.grid.frame.MainFrame;
import de.koller.aframe.util.thread.ICommand;
import de.koller.oceanFramework.tree.Child;
import de.koller.oceanFramework.tree.Sea3X;
import de.koller.oceanFramework.util.MIn;
import de.koller.oceanFramework.util.MOut;
import java.util.Random;

public class GridRoot extends Sea3X {
	
	public final Random rnd = new Random();
	private final GLoop loop = new GLoop( this );
	@Child public final MainFrame frame = new MainFrame();
	@Child public final EngineRoot engine = new EngineRoot();
	
	public final NewsModel news = new NewsModel( 100 );
	
	private long time = 0;

	@Override
	protected void init() {
		super.init();
		
		this.setIgnoreChildRender( true );
		
		MIn in = new MIn( "./grid/root.2m" );
		time = in.readLong( 1 )[0];
		in.close();
	}

	@Override
	protected void release() {
		super.release();
		
		MOut out = new MOut( "./grid/root.2m" );
		out.writeLong( time );
		out.close();
	}
	
	@Override
	protected void postChildInit() {
		super.postChildInit();
		
		//news.publish( new TextNewsEntry( time, "Wilkommen in " + GridStart.TITLE ) );
	}
	
	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
		
		time++;
	}
	
	public void start() {
		loop.begin();
	}
	
	public void stop() {
		loop.stop();
	}

	public void call( ICommand command ) {
		loop.thread.call( command );
	}

	public long getTime() {
		return time;
	}
	
}
